﻿using PBO.Data;
using PokemonBattle.BattleNetwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PBO.Game.BattleClass
{
    [Serializable()]
    public class PlayerBattleMove
    {

        private double _priority = -1;
        public PokemonIndex PMIndex { get; private set; }
        public TargetIndex TargetIndex { get; private set; }
        public Pokemon PM { get; private set; }
        public BattleMove Move { get; private set; }
        public byte MoveIndex { get; private set; }
        public List<Pokemon> Target { get; private set; }
        public int RandomPriority { get; set; }
        public double GetPriority()
        {
            return _priority;
        }

        public PlayerBattleMove(PlayerMove move)
            : this(move.Pokemon, move.Move, move.MoveIndex, move.Target)
        { }

        public PlayerBattleMove(PokemonIndex pmValue, BattleMove moveValue, byte index, TargetIndex targetValue)
        {
            this.Move = moveValue;
            this.MoveIndex = index;
            this.TargetIndex = targetValue;
            this.PMIndex = pmValue;
        }

        public void SetTarget(Battle battle, Random random)
        {
            if (this.Move != BattleMove.Attack)
                return;
            if (!this.PM.CanChooseMove)
            {
                if (this.PM.BattleState.nextMove == null)
                {
                    return;
                }
                else if (this.PM.BattleState.nextMove.Target == MoveTarget.随机)
                {
                    this.TargetIndex = TargetIndex.Random;
                }
                else
                {
                    this.TargetIndex = this.PM.LastTarget;
                }
            }
            this.PM.LastTarget = this.TargetIndex;
            this.Target = GetCurrentTarget(battle);
        }

        public void SetPM(Battle battle)
        {
            this.PM = battle.GetPokemonFromIndex(this.PMIndex);
        }

        public List<Pokemon> GetCurrentTarget(Battle battle)
        {
            Move selMove = null;
            if (this.MoveIndex == 0)
            {
                if (!this.PM.CanChooseMove)
                {
                    selMove = this.PM.BattleState.nextMove;
                }
            }
            else if (this.MoveIndex == Battle.StruggleIndex)
            {
                //selMove = null;
            }
            else
            {
                selMove = this.PM.GetSelMove(this.MoveIndex);
            }

            return battle.GetTargetsFromIndex(this.PM, selMove, this.TargetIndex);
        }

        public bool Pursuit
        {
            get
            {
                if (!this.PM.CanChooseMove)
                    return false;
                if (this.Move == BattleMove.Attack && this.MoveIndex != Battle.StruggleIndex && this.PM.GetSelMove(this.MoveIndex).Data.AddEffect2 == MoveAdditionalEffect.追击)
                {
                    return true;
                }
                return false;
            }
        }

        public bool FocusPunch
        {
            get
            {
                if (!this.PM.CanChooseMove)
                    return false;
                //气合拳
                if (this.Move == BattleMove.Attack && this.MoveIndex != Battle.StruggleIndex &&
                    this.PM.GetSelMove(this.MoveIndex).Data.Id == 264 &&
                    !(this.PM.GetSelTrait() == Trait.Truant && this.PM.BattleState.traitRaised) &&
                    !(this.PM.State == PokemonState.Sleep))
                {
                    return true;
                }
                return false;
            }
        }

        public bool Struggle
        {
            get
            {
                if (!this.PM.CanChooseMove)
                    return false;
                return this.Move == BattleMove.Attack && this.MoveIndex == Battle.StruggleIndex;
            }
        }

        public bool AttackMove
        {
            get
            {
                if (this.Move == BattleMove.Attack && (this.MoveIndex == 0 || this.MoveIndex == 5 || this.PM.GetSelMove(this.MoveIndex).Data.Category != MoveCategory.变化))
                {
                    return true;
                }
                return false;
            }
        }

        public void SetPriority(Random random)
        {
            _priority = Battle.CountAtkPriority(this.PM, this.MoveIndex);

            if (this.PM.GetItem() == Item.QuickClaw)
            {
                if (random.Next(10) < 2)
                {
                    this.PM.MyTeam.log.AppendMsg("QuickItem", this.PM.GetNameString(), Item.QuickClaw);
                    _priority += 0.5;
                }
            }
            else if (this.PM.BattleState.quickBerry)
            {
                _priority += 0.5;
                this.PM.BattleState.quickBerry = false;
            }
            else if (this.PM.GetItem() == Item.LaggingTail || this.PM.GetItem() == Item.FullIncense)
            {
                _priority -= 0.5;
            }
        }
    }
}
